<?
if(!POST) {
	$subpage = strtolower($params[2]);	
	if($subpage) $page->subpage = $page->page->subpages->$subpage;
} elseif(!FILE) {
	switch ($params[2]){
		case 'add':
			$urn = strtolower($_POST['urn']);
			if($page->page->subpages)
			foreach ($page->page->subpages as $link=>$i){
				if($urn == $link) $duplicate = true;	
			}
			if($duplicate)
				$page = uygulama::result('400',sprintf(uygulama::translate('"%s" subpage link is used!'),$urn));
			else {
				$page->page->subpages->$urn = new stdClass();
				$page->page->subpages->$urn->$lang->title = $_POST['title'];
				uygulama::set('data/'.$params[1].'/content', $page->page);
				$page = uygulama::result('200',sprintf(uygulama::translate('"%s" subpage is created!'),$_POST['title']));
			}
		break;
		case 'sort':
			$subpages = new stdClass();
			foreach (explode(',',$_POST['order']) as $url) {
				$subpages->$url = $page->page->subpages->$url;
			}
			$page->page->subpages = $subpages;
			uygulama::set('data/'.$params[1].'/content', $page->page);
			$page = uygulama::result('200',sprintf(uygulama::translate('"%s" subpage is ordered!'),$params[1]));
		break;
		case 'del':
			if($page->page->subpages->$_POST['urn']->media)
			foreach ($page->page->subpages->$_POST['urn']->media as $i){
				@unlink($i->thumb);
				@unlink($i->src);
			}
			unset($page->page->subpages->$_POST['urn']);
			uygulama::set('data/'.$params[1].'/content', $page->page);
			$page = uygulama::result('200',sprintf(uygulama::translate('"%s" subpage is deleted!'),$_POST['urn']));
		break;
		default:
			$lang = LANG;
			$subpage = strtolower($params[2]);
			// CONTENT MEDIA MANIPULATION
		    switch ($params[3]){
		        // MEDIA ALT
		        case 'alt':
		            if($_POST['ID'] && $page->page->subpages->$subpage->media->$_POST['ID']) {
		                $page->page->subpages->$subpage->media->$_POST['ID']->alt = $_POST['value'];
		                uygulama::set('data/'.$params[1].'/content', $page->page);
		                $page = uygulama::result('200',$_POST);
		            } else $page = uygulama::result('600',uygulama::translate('Media item not found!'));
		        break;
		        // MEDIA REMOVE
		        case 'removeMedia':
		            @unlink('data/'.$params[1].'/m'.$_POST['ID'].'.jpg');
		            @unlink('data/'.$params[1].'/t'.$_POST['ID'].'.jpg');
		            if($_POST['ID'] && $page->page->subpages->$subpage->media->$_POST['ID']) {
		                unset($page->page->subpages->$subpage->media->$_POST['ID']);
		                uygulama::set('data/'.$params[1].'/content', $page->page);
		                $page = uygulama::result('200');
		            } else $page = uygulama::result('600',uygulama::translate('Media item not found!'));
		        break;
		        // MEDIA SORT
				case 'sort':
					$media = new stdClass();
					foreach (explode(',',$_POST['order']) as $ID) {
						$media->$ID = $page->page->subpages->$subpage->media->$ID;
					}
					$page->page->subpages->$subpage->media = $media;
					uygulama::set('data/'.$params[1].'/content', $page->page);
					$page = uygulama::result('200',sprintf(uygulama::translate('"%s" media is ordered!'),$subpage));
				break;
		    };
			// Content data
			if(!$params[3]){
			$page->page->subpages->$subpage->$lang->title 		= $_POST['title'] 
						? stripslashes($_POST['title']) 		: $page->page->subpages->$subpage->$lang->title;
			$page->page->subpages->$subpage->$lang->description = $_POST['description'] 
						? stripslashes($_POST['description']) 	: $page->page->subpages->$subpage->$lang->description;
			$page->page->subpages->$subpage->$lang->keywords 	= $_POST['keywords'] 
						? stripslashes($_POST['keywords']) 		: $page->page->subpages->$subpage->$lang->keywords;
			$page->page->subpages->$subpage->$lang->content 	= $_POST['content'] 
						? stripslashes($_POST['content']) 		: $page->page->subpages->$subpage->$lang->content;
			// Save content
			uygulama::set('data/'.$params[1].'/content', $page->page);
			$page = uygulama::result('200',sprintf(uygulama::translate('"%s" subpage is saved!'),$params[1]));	
			}
		break;
	}
} else {	
	$subpage = strtolower($params[2]);
    $ext = '.'.end(explode(".",strtolower($_FILES['Filedata']['name'])));
    // MEDIA ID
    $ID = (@count($page->page->subpages->$subpage->media) +1).(microtime(1)*10000);
    //if(!$page->page->media) $page->page->media = new stdClass();
    $page->page->subpages->$subpage->media->$ID->type 	='image';
    $page->page->subpages->$subpage->media->$ID->thumb 	= 'data/'.$params[1].'/t'.$ID.'.jpg';
    $page->page->subpages->$subpage->media->$ID->alt 	= $_FILES['Filedata']['name'];
    $page->page->subpages->$subpage->media->$ID->src 	= 'data/'.$params[1].'/m'.$ID.'.jpg';
    $page->page->subpages->$subpage->media->$ID->tmp 	= 'data/'.$params[1].'/m'.$ID.$ext;
    // MEDIA UPLOAD
    uygulama::upload($_FILES['Filedata']['tmp_name'], $page->page->subpages->$subpage->media->$ID->tmp);
    // if type == image
    uygulama::imageresize($page->page->subpages->$subpage->media->$ID->tmp, $page->page->subpages->$subpage->media->$ID->src, 600, 600);
    uygulama::imageresize($page->page->subpages->$subpage->media->$ID->src, $page->page->subpages->$subpage->media->$ID->thumb, 160);
    // Delete tmp file
    if($page->page->subpages->$subpage->media->$ID->tmp != $page->page->subpages->$subpage->media->$ID->src)
    	@unlink($page->page->subpages->$subpage->media->$ID->tmp);
    // Clear tmp
    unset($page->page->subpages->$subpage->media->$ID->tmp);
    // SAVE CHANGES
    uygulama::set('data/'.$params[1].'/content', $page->page);
    // Set ID
    $page->page->subpages->$subpage->media->$ID->ID = $ID;
    // Uploaded media data
    echo json_encode($page->page->subpages->$subpage->media->$ID); exit;
}
?>
